package cn.schoolwow.flow.monitor.test.module.configProperties.flow;

import cn.schoolwow.flow.monitor.domain.FlowRecordable;
import cn.schoolwow.flow.monitor.entity.ConfigParameter;
import cn.schoolwow.flow.monitor.test.module.configProperties.domain.MailConfig;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.domain.query.instance.QueryInstance;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;
import org.springframework.context.ApplicationContext;

public class CheckMailConfigValueFlowRecord implements FlowRecordable {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        DAO starterDAO = applicationContext.getBean("starterDAO", DAO.class);
        MailConfig mailConfig = flowContext.checkInstanceData(MailConfig.class);

        ConfigParameter configParameter = starterDAO.query(ConfigParameter.class)
                .addQuery("className", MailConfig.class.getName())
                .execute()
                .getOne();
        JSONObject value = configParameter.getValue();
        for(String key: value.keySet()){
            Object expectFieldValue = value.get(key);
            Object actualFieldValue = QueryInstance.newQuery(mailConfig)
                    .execute()
                    .getSingleFieldValue(key);
            Assert.assertEquals(expectFieldValue, actualFieldValue);
        }
    }

    @Override
    public String name() {
        return "测试邮件记录信息是否匹配";
    }

}
